package com.atguigu.rabbitmq.consumer.listeners;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class TestConsumerListener {

    @RabbitListener(
            bindings = @QueueBinding(
                    exchange = @Exchange(value = "test_exchange_01", durable = "true"),
                    key = {"info","error"},
                    value = @Queue(value = "test_queue_01", durable = "true")
            )
    )
    public void a(Channel channel, Message message, String str) throws IOException {
        try {
            byte[] body = message.getBody();
            String s = new String(body);
            System.out.println("===========test_queue_01：" + s + "========");
            System.out.println("==================监听调用业务层代码======================");
             int i = 1/0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        }catch (Exception e){
            // 执行失败代码
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true,true);
            System.out.println("执行失败了");
        }
    }
}